Ext.define('SYS.controller.PermissionController', {
    extend: 'Ext.app.Controller',
    mixins: {
     	classSave: 'Ext.ux.CommonSave'
    },
    stores: [
             'PermissionStore'
         ],
    views: [
            'permission.List',
            'permission.Add',
            'permission.Update',
            'permission.View'
        ],
    refs : [{
		ref : 'permissionlist',
		selector : 'permissionlist'
	},{
		ref : 'permissionAdd',
		selector : 'permissionAdd',
		autoCreate: true,
        xtype: 'permissionAdd'
	},{
		ref : 'permissionUpdate',
		selector : 'permissionUpdate',
		autoCreate: true,
        xtype: 'permissionUpdate'
	}],
	grid : function(){
		return this.getPermissionlist();// refs 首字母大写
	},
	store : function(){
		return this.getPermissionStoreStore();
	},
	save_url : 'perm/create.do',
	update_url: 'perm/update.do',
	cheSel:function(){
		var grid = this.grid();
		var record = grid.getSelectionModel().getSelection(); 
		if(record.length!=1){
			Ext.MessageBox.alert('提示', "请选择一条记录！");
			return false;
		}else{
			return record[0];
		}
	},
    init: function() {
        this.control({
           'permissionlist > toolbar #comboboxBtn': {//每页显示数
        	   'change': this.changepage
           },
           'permissionlist button[action=find]': {//搜索
               click: this.find
           },
           'permissionlist button[action=add]':{
        	   click: function(){
        	   	this.getPermissionAdd().show();
        	   }
           },
           'permissionlist button[action=edit]':{//修改
        	   click: function(){
				   if(this.cheSel()){
					   var view = this.getPermissionUpdate().show();
					   view.down('form').loadRecord(this.cheSel());
				   }
        	   }
           },
           'permissionlist button[action=delete]':{//删除
           		click: this.del
           },
           'permissionAdd button[action=save]': {//单位新增保存
               click: this.add_save
           },
           'permissionUpdate button[action=save]': {//修改保存
               click: this.update_save
           },
           'permissionlist button[action=stop]':{
           		click: function(){
			    	var obj = this;
			    	pubOper(obj,'提示','perm/stop.do','确定停用？','id');
           		}
           },
           'permissionlist button[action=start]':{
           		click: function(){
			    	var obj = this;
			    	pubOper(obj,'提示','perm/start.do','确定启用？','id');
           		}
           }
        });
    },
    changepage : function(Field, newValue, oldValue){ //设置每页显示
    	var store = this.store();
    	store.pageSize = newValue;
    	store.on('beforeload', function (store, options) {
            var new_params = { "limit":newValue};
            Ext.apply(store.proxy.extraParams, new_params);
            });
        store.loadPage(1);
    },
    find : function(){//搜索
    	var store = this.store();
    	store.on('beforeload', function (store, options) {
                var new_params = {
					'name':Ext.getCmp('name_s').value,
                	'parentId':Ext.getCmp('parentId_s').value
                }
                Ext.apply(store.proxy.extraParams, new_params);
                });
		 store.loadPage(1); 
    },
    add_save: function(button){ //表单保存
    	this.common_save(button,this.save_url);
    },
    update_save : function(button){
    	this.common_save(button,this.update_url);
    },
    del : function(){
    	var obj = this;
    	pubOper(obj,'提示','perm/delete.do','确定删除？','id');
  	}
});
